'use strict';
let menuButtonInfo = {
	top: 6, // 胶囊按钮 TOP 值
	width: 0, // 胶囊按钮宽度
	height: 38 // 胶囊按钮高度
};
// #ifdef MP
menuButtonInfo = uni.getMenuButtonBoundingClientRect();
// #endif
// #ifdef H5
menuButtonInfo.top = 6;
// #endif

class Common {
	constructor(arg) {
		this.navbar = menuButtonInfo;
		this.menuButtonInfo = menuButtonInfo;
		this.navbarTitleHeight = menuButtonInfo.height;
		this.navbarHeight = menuButtonInfo.top + menuButtonInfo.height + 6;

		console.log('this.navbar', this.navbar);
	}

	/**
	 * @description 是否登录
	 */
	isLogin() {
		let user = uni.getStorageSync("users") || {}
		return Object.keys(user).length > 0
	}
	/**
	 * @description 打开隐私协议
	 */
	openPrivacyContract() {
		// 打开隐私协议页面
		uni.openPrivacyContract({
			success: () => {},
			fail: () => {}
		});
	}


	/**
	 * @description 日期格式化
	 */
	format(date, fmt) {
		if (typeof date === 'string') {
			date = date.replace(/\.|\-/g, '/');
		}
		if (typeof date !== 'object') {
			date = new Date(date);
		}

		fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
		let o = {
			"M+": date.getMonth() + 1, //月份   
			"d+": date.getDate(), //日   
			"h+": date.getHours(), //小时   
			"m+": date.getMinutes(), //分   
			"s+": date.getSeconds(), //秒
			"q+": Math.floor((date.getMonth() + 3) / 3), //季度
			"S": date.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt))
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (let k in o)
			if (new RegExp("(" + k + ")").test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k])
					.length)));
		return fmt;
	}



	/**
	 * @description 数值格式化
	 */
	format_number(number) {
		try {
			number = parseFloat(number);

			if (isNaN(number)) return 0;

			if (number > 10000 * 1000) {
				return (number / (10000 * 10000)).toFixed(1) + '亿';
			}

			if (number > 10000) {
				return (number / (10000 * 1)).toFixed(1) + '万';
			}

			return number;
		} catch (e) {
			return number;
		}
	}


	/**
	 * @description 消息提示 toast
	 */
	msg(title, duration = 1500, mask = false, icon = 'none') {
		if (!title) return;
		if (typeof title !== 'string') title = JSON.stringify(title);

		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	}
	/**
	 * @description 消息提示 alert
	 */
	alert(content, callback) {
		if (typeof content !== 'string') content = JSON.stringify(content);

		uni.showModal({
			title: '提示',
			content: content,
			showCancel: false,
			success(res) {
				if (res.confirm) {
					if (typeof callback === 'function') {
						callback();
					}

				} else if (res.cancel) {}
			}
		})
	}


	/**
	 * @description 去除两端空格
	 * */
	trim(str) {
		return str.replace(/\s+/g, "");
	}


	/**
	 * @description 打印 info 日志
	 * */
	info(msg) {
		console.info(msg)
	}

	/**
	 * @description 当前页面数组
	 * */
	pages() {
		return getCurrentPages();
	}

	/**
	 * @description 返回上一级页面|跳转首页
	 * */
	back() {
		if (getCurrentPages().length > 1) {
			uni.navigateBack({})
			return
		}

		this.toHome();
	}

	/**
	 * @description 指定元素选择器 offset
	 */
	offset(selector, callback) {
		let query = uni.createSelectorQuery().select(selector);
		// console.log('offset query', query);
		if (typeof callback === 'function') {
			query.boundingClientRect((res) => {
				callback(res);
			});
		}
	}

	/**
	 * @description 超时
	 */
	timerout(callback, timer = 1000) {
		let _timer = setTimeout(() => {
			if (typeof callback === 'function') {
				callback();
			}
			clearTimeout(_timer);
		}, timer);
	}

	
	/**
	 * @description 跳转指定 path 页
	 */
	toPage(path, req = {}) {
		if (Object.keys(req).includes('islogin')) {
			if (!req.islogin) {
				this.toLogin(req.redirect);
				return;
			}
		}

		uni.navigateTo({
			url: path,
			fail: () => {
				uni.switchTab({
					url: path
				})
			}
		})
	}

	/**
	 * @description 复制
	 */
	toCopy(content) {
		uni.setClipboardData({
			data: content,
			success: (res) => {
				uni.showToast({
					title: '复制成功'
				});
			}
		});
	}

	/**
	 * @description 自定义状态栏高度
	 */
	navbarHeight() {
		const {
			platform,
			statusBarHeight
		} = uni.getSystemInfoSync();
		console.log("platformplatformplatformplatformplatformplatformplatform", platform)
		let height = statusBarHeight;
		height += 4;
		if (platform == 'android') {
			height += 4;
		}

		return height + 32 + 6;
	}


	// 获取当前 page
	getCurPage() {
		const pages = this.pages();
		const curPage = pages[pages.length - 1];

		return curPage;
	}


	getDayOfWeek(dateString) {
		const date = new Date(dateString);
		const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
		return weekdays[date.getDay()]; // getDay() 返回 0（星期日）到 6（星期六）
	}
	 getDayFromTimestamp(timestamp) {
	    // 创建一个新的 Date 对象
	    const date = new Date(timestamp);
	    
	    // 获取星期几（0表示周日，1表示周一，...，6表示周六）
	    const day = date.getDay();
	    
	    // 创建一个数组来存储星期的名称
	    const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
	    
	    // 返回对应的星期名称
	    return daysOfWeek[day];
	}
}

export default new Common()